package com.qf.smsplatform.strategy.strategy.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.OperatorConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.common.util.CheckMobileOperatorUtil;
import com.qf.smsplatform.strategy.client.CacheClient;
import com.qf.smsplatform.strategy.strategy.Strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * projectName: sms-platform
 *
 * @author: 黄睿武
 * time: 2021/8/13 19:40
 * description:
 */


/**
 * 手机号号段补全策略
 */
@Service(value = "phase")
@Slf4j
public class PhaseStrategyImpl implements Strategy {

    //省市位置,填充0
    private final int UNKNOWN_PROVINCE_CITY = 0;

    @Autowired
    private CacheClient cacheClient;



    /**
     * 所有策略模块必须有的一个方法,用于校验或补全短信内容(submit对象)
     *
     * @param submit 短信内容
     * @return true代表通过, false校验未通过
     */
    @Override
    public boolean execute(StandardSubmit submit) {
        log.info("[策略模块-号段补全]  开始执行.......");
        //1.----------------查询手机号运营商------------------
        String mobile = submit.getDestMobile();
        if (CheckMobileOperatorUtil.isChinaMobilePhoneNum(mobile)) {
            //移动
            submit.setOperatorId(OperatorConstants.MOBILE_PHONE);

        }else if (CheckMobileOperatorUtil.isChinaUnicomPhoneNum(mobile)){
            //联通
            submit.setOperatorId(OperatorConstants.UNICOM_PHONE);

        }else if (CheckMobileOperatorUtil.isChinaTelecomPhoneNum(mobile)){
            //电信
            submit.setOperatorId(OperatorConstants.TELECOM_PHONE);

        }else{
            //未知
            submit.setOperatorId(OperatorConstants.UNKNOW);

        }

        //-----------------------2.查询手机号归属地----------------------
        //2.1截取出手机号的前7位
        String phase = mobile.substring(0, 7);

        //2.2封装key
        String key  = CacheConstants.CACHE_PREFIX_PHASE + phase;

        //2.3通过cacheService查询到value
        String provinceAndCity = cacheClient.getObject(key);

        //2.4判断不为空
        if(StringUtils.isEmpty(provinceAndCity)){
            submit.setProvinceId(UNKNOWN_PROVINCE_CITY);
            submit.setCityId(UNKNOWN_PROVINCE_CITY);
        }else{
            //2.6采用,分隔,0索引位置是省,1索引位置是市
            String[] provinceCity = provinceAndCity.split(",");
            if(provinceCity.length == 2){
                submit.setProvinceId(Integer.valueOf(provinceCity[0]));
                submit.setCityId(Integer.valueOf(provinceCity[1]));
            }else{
                submit.setProvinceId(UNKNOWN_PROVINCE_CITY);
                submit.setCityId(UNKNOWN_PROVINCE_CITY);
            }

        }



        //-------------------------3.查询出当前手机号要使用哪个通道发送-------------------------
        //3.1调用缓存模块,根据CLIENT:ClientID查询当前客户绑定的所有通道
        Set<Map> channels = cacheClient.smembers(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + submit.getClientID());

        //3.2根据手机号的运营商匹配通道中的channelType,如果没有匹配上,选择全网通
        int operatorId = submit.getOperatorId();
        if (channels != null && channels.size() > 0){
            for (Map channel : channels) {
                //手机号的运营商匹配通道的channelType
                int channelType = (Integer) channel.get("channelType");
                if(channelType == 0){
                    //全网通
                    //3.3-------封装getewayID,srcNumber,fee------
                    setChannelData(channel, submit);
                    continue;
                }
                if(operatorId == channelType){
                    //当前手机号匹配上了通道
                    //3.3-------封装getewayID,srcNumber,fee------

                    setChannelData(channel, submit);
                    break;
                }
            }
        }




        log.info("[策略模块-号段补全]  执行成功.......");
        return true;
    }


    /**
     * 选择好了指定的通道
     * @param channel
     * @param submit
     */
    private void setChannelData(Map channel, StandardSubmit submit) {

        //封装getewayID,srcNumber,fee
        submit.setGatewayID((Integer) channel.get("channelId"));
        String srcNumber = channel.get("spNumber") + "" + channel.get("extendNumber");
        submit.setSrcNumber(srcNumber);
        submit.setFee(Long.parseLong(channel.get("price") + ""));


    }
}
